package com.wanmait.recruitment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.mapper.UserinfoMapper;
import com.wanmait.recruitment.pojo.Userinfo;
import com.wanmait.recruitment.service.MailService;
import com.wanmait.recruitment.service.UserinfoService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class UserinfoServicelmpl implements UserinfoService {

    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private MailService mailService;

    @Override
    public void insert(Userinfo object) {

    }
    @Override
    public Userinfo getUserByActiveCode(String activeCode) {
        return userinfoMapper.selectUserByActiveCode(activeCode);
    }
    //注册用户
    @Override
    @CacheEvict(value = "userinfoCache",allEntries = true)
    public void register(Userinfo userinfo) {
         userinfoMapper.register(userinfo);
//         //获取激活码
//        String code= userinfo.getActiveCode();
//        System.out.println("激活码"+code);
//        //主题
//        String subject = "壮壮招聘网的激活邮件";
//        //上面的激活码发送到用户注册邮箱
//        String context = "<a href=\"http://localhost:8080/zhuang/checkCode?code="+code+"\">激活请点击:"+code+"</a>";
//        //发送激活邮件
//        mailService.sendMimeMail (userinfo.getEmail(),subject,context);

    }
    //激活状态
    @Override
    @CacheEvict(value = "userinfoCache",allEntries = true)
    public void modify(Userinfo userinfo) {
        userinfoMapper.update(userinfo);
    }


    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(Userinfo object) {


    }
    //验证用户名是否存在
    @Override
    public String checkUsername(String username) {

     return userinfoMapper.checkUsername(username);
    }

    @Override
    public Userinfo login(String email, String userpass) {
        return userinfoMapper.login(email, userpass);
    }

    @Override
    public Userinfo findByUsername(String email) {
        return userinfoMapper.findByUsername(email);
    }

    @Override
    @CacheEvict(value = "userinfoCache",allEntries = true)
    public int updateUserPass(String email, String userpass) {
        return userinfoMapper.updateUserpass(email, userpass);
    }

    @Override
    @CacheEvict(value = "userinfoCache",allEntries = true)
    public int updatenowResumeCount() {
        return userinfoMapper.updatenowResumeCount();
    }

    @Override
    @CacheEvict(value = "userinfoCache",allEntries = true)
    public void updateNowResumeCount(Integer id) {
        userinfoMapper.updateNowResumeCount(id);
    }

    @Override
    @CacheEvict(value = "userinfoCache",allEntries = true)
    public void updateSubscribeCount(Integer id,Integer change ) {
        userinfoMapper.updateSubscribeCount(id,change);
    }

    @Override
    public Userinfo findById(Integer id) {

        return userinfoMapper.findById(id);
    }

    @Override
    public List<Userinfo> findAll() {

        return null;
    }

    @Override
    public List<Userinfo> findAll(Userinfo object) {
        return null;
    }

    @Override
    public PageInfo<Userinfo> findPage(int pageNum, int pageSize) {
        return null;
    }

    @Override
    @Cacheable("userinfoCache")
    public PageInfo<Userinfo> findPage(Userinfo userinfo,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Userinfo> userinfos = userinfoMapper.findAll(userinfo);
        PageInfo<Userinfo> pageInfo = new PageInfo<>(userinfos,7);
        return pageInfo;
    }

    @Override
    @CacheEvict(value = "userinfoCache",allEntries = true)
    public void updateEnable(Userinfo userinfo) {
        userinfoMapper.updateEnable(userinfo);
    }

    @Override
    @CacheEvict(value = "userinfoCache",allEntries = true)
    public void batchDel(String  idArr) {
        String[] ids = idArr.split(",");
        List<Integer> list = new ArrayList<>();
        for (int i=0;i<ids.length;i++){
            list.add(Integer.parseInt(ids[i]));
        }
        userinfoMapper.batchDel(list);
    }

    @Override
    @CacheEvict(value = "userinfoCache",allEntries = true)
    public void updatePassword(String userpass, Integer id) {
        userinfoMapper.updatepassword(userpass,id);
    }

}
